home *** CD-ROM | disk | FTP | other *** search
- unit DCRichEdit;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ComCtrls;
-
- type
- TDCRichEdit = class(TRichEdit)
- private
- FRTF, FSavedRTF: String;
- //Fake RTF property reader
- procedure ReadData(Reader: TReader);
- //Fake RTF property writer
- procedure WriteData(Writer: TWriter);
- protected
- //Routine to define fake RTF property
- procedure DefineProperties(Filer: TFiler); override;
- procedure CreateWnd; override;
- procedure DestroyWnd; override;
- published
- //Stop the Lines property from storing itself
- property Lines stored False;
- end;
-
- //Turn Lines property into string with formatting
- function RichEditLinesToString(RichEdit: TCustomRichEdit): String;
- //Take RTF string and give it to Lines property
- procedure StringToRichEditLines(
- const AString: String; RichEdit: TCustomRichEdit);
- //Copy RTF from one rich edit control to another
- procedure RichEditLinesToRichEditLines(Src, Dest: TCustomRichEdit);
-
- implementation
-
- type
- TRichEditAccess = class(TCustomRichEdit);
-
- //The parameters for these two routines are declared as TCustomRichEdit
- //This is to allow as many rich edit descendants to work with this
- //code as possible. However, in TCustomRichEdit, the inherited
- //TCustomMemo.Lines public property is more accessible than the new
- //protected TCustomRichEdit.Lines property. The access class above
- //newer protected property to be accessed.
- function RichEditLinesToString(RichEdit: TCustomRichEdit): String;
- var
- Stream: TMemoryStream;
- begin
- if not Assigned(RichEdit) then
- begin
- Result := '';
- Exit
- end;
- //Create a memory stream
- Stream := TMemoryStream.Create;
- try
- //Copy Lines into stream, inc. formatting
- TRichEditAccess(RichEdit).Lines.SaveToStream(Stream);
- //Set stream pointer to BOF
- Stream.Position := 0;
- //Read from stream into a string
- SetString(Result, PChar(Stream.Memory), Stream.Size)
- finally
- Stream.Free
- end
- end;
-
- procedure StringToRichEditLines(
- const AString: String; RichEdit: TCustomRichEdit);
- var
- Stream: TStream;
- begin
- if not Assigned(RichEdit) then
- Exit;
- if Length(AString) = 0 then
- RichEdit.Lines.Clear
- else
- begin
- //Create a memory stream
- Stream := TMemoryStream.Create;
- try
- //Copy RTF data into stream
- Stream.Write(AString[1], Length(AString));
- //Set stream pointer to BOF
- Stream.Position := 0;
- //Load RTF into Lines property, inc. formatting
- TRichEditAccess(RichEdit).Lines.LoadFromStream(Stream)
- finally
- Stream.Free
- end
- end
- end;
-
- procedure RichEditLinesToRichEditLines(Src, Dest: TCustomRichEdit);
- var
- SrcStream, DestStream: TStream;
- begin
- SrcStream := TMemoryStream.Create;
- try
- DestStream := TMemoryStream.Create;
- try
- Src.Lines.SaveToStream(SrcStream);
- SrcStream.Position := 0;
- DestStream.CopyFrom(SrcStream, 0);
- DestStream.Position := 0;
- Dest.Lines.LoadFromStream(DestStream)
- finally
- DestStream.Free
- end
- finally
- SrcStream.Free
- end
- end;
-
- procedure TDCRichEdit.ReadData(Reader: TReader);
- begin
- //Read RTF from form file and give it to Lines, with formatting (if required)
- if PlainText then
- Lines.Text := Reader.ReadString
- else
- StringToRichEditLines(Reader.ReadString, Self);
- end;
-
- procedure TDCRichEdit.WriteData(Writer: TWriter);
- begin
- //Get string containing text and formatting (if required)
- if PlainText then
- Writer.WriteString(Lines.Text)
- else
- Writer.WriteString(RichEditLinesToString(Self))
- end;
-
- procedure TDCRichEdit.DefineProperties(Filer: TFiler);
-
- function DoWrite: Boolean;
- begin
- FRTF := RichEditLinesToString(Self);
- if Assigned(Filer.Ancestor) then
- begin
- Result := True;
- if Filer.Ancestor is TDCRichEdit then
- Result := FRTF <> TDCRichEdit(Filer.Ancestor).FRTF
- end
- else
- Result := Lines.Count > 0;
- end;
-
- begin
- inherited;
- Filer.DefineProperty('RTF', ReadData, WriteData, DoWrite)
- end;
-
- procedure TDCRichEdit.CreateWnd;
- begin
- inherited;
- if not PlainText then
- StringToRichEditLines(FSavedRTF, Self);
- end;
-
- procedure TDCRichEdit.DestroyWnd;
- begin
- if not PlainText then
- FSavedRTF := RichEditLinesToString(Self);
- inherited
- end;
-
- end.
-